#include "graphicsscene.h"

#include <QDebug>
#include <cstdlib>

GraphicsScene::GraphicsScene(QObject *parent) : QGraphicsScene(parent)
{
    this->init();
    this->p_timer = new QTimer;
    connect(this->p_timer,&QTimer::timeout,this,&GraphicsScene::showMouse);
    //p_timer->start(1000);
}
void GraphicsScene::init()
{
    for(int i = 0;i<4;i++)
    {
        for(int j = 0;j<4;j++)
        {
            this->items[i][j] = new GraphicsItem;
            this->items[i][j]->setPos(i*this->items[i][j]->boundingRect().width(),j*this->items[i][j]->boundingRect().height());
            this->items[i][j]->setMouse(false);
            this->items[i][j]->setStart(false);
            this->addItem(this->items[i][j]);

        }
    }
}

//随机显示老鼠

void GraphicsScene::showMouse()
{
    //初始化一次
    this->init();
    //设置最多一次同时显示个数
    int syn_count = rand()%4;
    for(int i = 0;i<syn_count;i++)
    {
        int col = rand()%4;
        int row = rand()%4;
        this->items[col][row]->setPixmap(QPixmap(":/mouse/images/mouse_up.png"));
        this->items[col][row]->setMouse(true);
        this->items[col][row]->setStart(true);
    }
}

void GraphicsScene::startGame()
{
    this->p_timer->start(1000);
}

void GraphicsScene::suspendGame()
{
    //暂停
    this->p_timer->stop();

}
void GraphicsScene::stopGame()
{
    this->p_timer->stop();
    //停止后重新初始化
    this->init();
}
